﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Linq;
using System.Text;
using System.Security;

namespace Gmantis.Controls.DataGrid
{
    public interface IDataGridInputHandlingStrategy
    {
        // Methods
        void HandleKeyDown(System.Windows.Input.KeyEventArgs e);
    }

    public class DataGridDefaultInputHandlingStrategy : IDataGridInputHandlingStrategy
    {
        // Methods
        public DataGridDefaultInputHandlingStrategy(DataGrid dataGrid)
        {
            this.DataGrid = dataGrid;
        }

        public virtual DataGridCell GetBottomCell(DataGridCell currentCell)
        {
            if (currentCell != null)
            {
                DataGrid dataGrid = currentCell.DataGrid;
                foreach (DataGridRow row in dataGrid.Rows.GetVisibleRowsFromBottom(false))
                {
                    if (row.IsSelectable)
                    {
                        return dataGrid[row, currentCell.Column];
                    }
                }
            }
            return null;
        }

        public virtual DataGridRow GetBottomVisibleRow(DataGrid dataGrid)
        {
            foreach (DataGridRow row in dataGrid.Rows.GetVisibleRowsFromBottom(false))
            {
                if (row.IsSelectable)
                {
                    return row;
                }
            }
            return null;
        }

        public virtual string GetClipboardContent()
        {
            bool copyRowHeaders = (((byte)this.DataGrid.ClipboardCopyMode) & 4) == 4;
            bool copyColumnHeaders = (((byte)this.DataGrid.ClipboardCopyMode) & 2) == 2;
            return this.GetFormattedSelection("\t", "\r\n", copyRowHeaders, copyColumnHeaders);
        }

        private DataGridCell GetFirstEditableCell(DataGridRow row)
        {
            if (row != null)
            {
                DataGrid dataGrid = row.DataGrid;
                foreach (DataGridColumn column in (from column in dataGrid.Columns
                                                   orderby column.DisplayIndex
                                                   select column).ToArray<DataGridColumn>())
                {
                    if (column.ActualIsEditable && column.IsSelectable)
                    {
                        return dataGrid[row, column];
                    }
                }
            }
            return null;
        }

        protected string GetFormattedSelection(string separator, string lineSeparator, bool copyRowHeaders, bool copyColumnHeaders)
        {
            StringBuilder builder = new StringBuilder();
            bool flag = true;
            bool flag2 = true;
            for (int i = copyColumnHeaders ? -1 : 0; i < this.DataGrid.Selection.SelectedRows.Count; i++)
            {
                DataGridRow row = null;
                if (i >= 0)
                {
                    row = this.DataGrid.Selection.SelectedRows[i];
                }
                if (!flag)
                {
                    builder.Append(lineSeparator);
                }
                flag2 = true;
                for (int j = copyRowHeaders ? -1 : 0; j < this.DataGrid.Selection.SelectedColumns.Count; j++)
                {
                    DataGridColumn column = null;
                    if (j >= 0)
                    {
                        column = this.DataGrid.Selection.SelectedColumns[j];
                    }
                    if (!flag2)
                    {
                        builder.Append(separator);
                    }
                    if (j != -1)
                    {
                        if (i == -1)
                        {
                            string columnText = column.GetColumnText();
                            builder.Append(columnText);
                        }
                        else if (row.Type == DataGridRowType.Group)
                        {
                            if (flag2)
                            {
                                builder.Append(((DataGridGroupRow)row).GetGroupText());
                            }
                        }
                        else
                        {
                            DataGridCell item = this.DataGrid[row, column];
                            if (this.DataGrid.Selection.SelectedCells.Contains(item))
                            {
                                builder.Append(string.Format("{0}", item.Text));
                            }
                        }
                    }
                    flag2 = false;
                }
                flag = false;
            }
            return builder.ToString();
        }

        private DataGridCell GetLastEditableCell(DataGridRow row)
        {
            if (row != null)
            {
                DataGrid dataGrid = row.DataGrid;
                foreach (DataGridColumn column in (from column in dataGrid.Columns
                                                   orderby column.DisplayIndex descending
                                                   select column).ToArray<DataGridColumn>())
                {
                    if (column.ActualIsEditable && column.IsSelectable)
                    {
                        return dataGrid[row, column];
                    }
                }
            }
            return null;
        }

        public virtual DataGridCell GetLeftMostCell(DataGridCell currentCell)
        {
            if (currentCell != null)
            {
                DataGrid dataGrid = currentCell.DataGrid;
                DataGridColumn leftMostVisibleColumn = this.GetLeftMostVisibleColumn(dataGrid);
                if (leftMostVisibleColumn != null)
                {
                    return dataGrid[currentCell.Row, leftMostVisibleColumn];
                }
            }
            return null;
        }

        public virtual DataGridColumn GetLeftMostVisibleColumn(DataGrid dataGrid)
        {
            foreach (DataGridColumn column in dataGrid.Columns.GetVisibleColumnsFromLeft())
            {
                if (column.IsSelectable)
                {
                    return column;
                }
            }
            return null;
        }

        public virtual DataGridCell GetNextEditableCell(DataGridCell currentCell)
        {
            if (currentCell != null)
            {
                DataGrid dataGrid = currentCell.DataGrid;
                int displayIndex = currentCell.Column.DisplayIndex;
                DataGridColumn[] columnArray = (from column in dataGrid.Columns
                                                orderby column.DisplayIndex
                                                select column).ToArray<DataGridColumn>();
                for (int i = 0; i < columnArray.Length; i++)
                {
                    int index = ((i + displayIndex) + 1) % columnArray.Length;
                    DataGridColumn column = columnArray[index];
                    if ((column.ActualIsEditable && column.IsSelectable) && (column.Visibility == Visibility.Visible))
                    {
                        bool flag = ((i + displayIndex) + 1) >= columnArray.Length;
                        DataGridRow currentRow = currentCell.Row;
                        if (flag)
                        {
                            do
                            {
                                currentRow = this.GetNextVisibleRow(currentRow);
                                if (currentRow == null)
                                {
                                    return null;
                                }
                            }
                            while ((currentRow.Type != DataGridRowType.Item) && (currentRow.Type != DataGridRowType.New));
                        }
                        if (currentRow != null)
                        {
                            return dataGrid[currentRow, column];
                        }
                        break;
                    }
                }
            }
            return null;
        }

        public virtual DataGridColumn GetNextVisibleColumn(DataGridColumn currentColumn)
        {
            if (currentColumn != null)
            {
                foreach (DataGridColumn column in currentColumn.DataGrid.Columns.GetVisibleColumnsFromIndex(currentColumn.DisplayIndex + 1, GeneratorDirection.Forward))
                {
                    if (column.IsSelectable)
                    {
                        return column;
                    }
                }
            }
            return null;
        }

        public virtual DataGridRow GetNextVisibleRow(DataGridRow currentRow)
        {
            if (currentRow != null)
            {
                foreach (DataGridRow row in currentRow.DataGrid.Rows.GetVisibleRowsFromIndex(currentRow.Index + 1, GeneratorDirection.Forward, false))
                {
                    if (row.IsSelectable)
                    {
                        return row;
                    }
                }
            }
            return null;
        }

        public virtual DataGridCell GetPreviousEditableCell(DataGridCell currentCell)
        {
            if (currentCell != null)
            {
                DataGrid dataGrid = currentCell.DataGrid;
                int displayIndex = currentCell.Column.DisplayIndex;
                DataGridColumn[] columnArray = (from column in dataGrid.Columns
                                                orderby column.DisplayIndex
                                                select column).ToArray<DataGridColumn>();
                for (int i = 0; i < columnArray.Length; i++)
                {
                    int index = (((displayIndex - 1) - i) + columnArray.Length) % columnArray.Length;
                    DataGridColumn column = columnArray[index];
                    if ((column.ActualIsEditable && column.IsSelectable) && (column.Visibility == Visibility.Visible))
                    {
                        bool flag = ((displayIndex - 1) - i) < 0;
                        DataGridRow currentRow = currentCell.Row;
                        if (flag)
                        {
                            do
                            {
                                currentRow = this.GetPreviousVisibleRow(currentRow);
                                if (currentRow == null)
                                {
                                    return null;
                                }
                            }
                            while ((currentRow.Type != DataGridRowType.Item) && (currentRow.Type != DataGridRowType.New));
                        }
                        if (currentRow != null)
                        {
                            return dataGrid[currentRow, column];
                        }
                        break;
                    }
                }
            }
            return null;
        }

        public virtual DataGridColumn GetPreviousVisibleColumn(DataGridColumn currentColumn)
        {
            if (currentColumn != null)
            {
                foreach (DataGridColumn column in currentColumn.DataGrid.Columns.GetVisibleColumnsFromIndex(currentColumn.DisplayIndex - 1, GeneratorDirection.Backward))
                {
                    if (column.IsSelectable)
                    {
                        return column;
                    }
                }
            }
            return null;
        }

        public virtual DataGridRow GetPreviousVisibleRow(DataGridRow currentRow)
        {
            if (currentRow != null)
            {
                foreach (DataGridRow row in currentRow.DataGrid.Rows.GetVisibleRowsFromIndex(currentRow.Index - 1, GeneratorDirection.Backward, false))
                {
                    if (row.IsSelectable)
                    {
                        return row;
                    }
                }
            }
            return null;
        }

        public virtual DataGridCell GetRightMostCell(DataGridCell currentCell)
        {
            if (currentCell != null)
            {
                DataGrid dataGrid = currentCell.DataGrid;
                DataGridColumn rightMostVisibleColumn = this.GetRightMostVisibleColumn(dataGrid);
                if (rightMostVisibleColumn != null)
                {
                    return dataGrid[currentCell.Row, rightMostVisibleColumn];
                }
            }
            return null;
        }

        public virtual DataGridColumn GetRightMostVisibleColumn(DataGrid dataGrid)
        {
            foreach (DataGridColumn column in dataGrid.Columns.GetVisibleColumnsFromRight())
            {
                if (column.IsSelectable)
                {
                    return column;
                }
            }
            return null;
        }

        public virtual DataGridCell GetTopCell(DataGridCell currentCell)
        {
            if (currentCell != null)
            {
                DataGrid dataGrid = currentCell.DataGrid;
                foreach (DataGridRow row in dataGrid.Rows.GetVisibleRowsFromTop(false))
                {
                    if (row.IsSelectable)
                    {
                        return dataGrid[row, currentCell.Column];
                    }
                }
            }
            return null;
        }

        public virtual DataGridRow GetTopVisibleRow(DataGrid dataGrid)
        {
            foreach (DataGridRow row in dataGrid.Rows.GetVisibleRowsFromTop(false))
            {
                if (row.IsSelectable)
                {
                    return row;
                }
            }
            return null;
        }

        public virtual bool GoToBottomCell(DataGridCell currentCell, bool shift, bool ctrl)
        {
            DataGridCell bottomCell = this.GetBottomCell(currentCell);
            if (bottomCell != null)
            {
                this.SetCurrentCell(bottomCell, shift, ctrl);
                return true;
            }
            return false;
        }

        public bool GoToDownCell(DataGridCell currentCell)
        {
            return this.GoToDownCell(currentCell, KeyboardUtil.Shift, KeyboardUtil.Ctrl);
        }

        public virtual bool GoToDownCell(DataGridCell currentCell, bool shift, bool ctrl)
        {
            if (currentCell != null)
            {
                DataGrid dataGrid = currentCell.DataGrid;
                foreach (DataGridRow row in dataGrid.Rows.GetVisibleRowsFromIndex(dataGrid.Viewport.Range(currentCell).BottomRightCell.Row.Index + 1, GeneratorDirection.Forward, false))
                {
                    if (row.IsSelectable)
                    {
                        return this.SetCurrentCell(currentCell.DataGrid[row, currentCell.Column], shift, ctrl);
                    }
                }
            }
            return false;
        }

        public virtual bool GoToFirstCell(DataGrid dataGrid, bool shift, bool ctrl)
        {
            return this.SetCurrentCell(dataGrid[this.GetTopVisibleRow(dataGrid), this.GetLeftMostVisibleColumn(dataGrid)], shift, ctrl);
        }

        public virtual bool GoToLastCell(DataGrid dataGrid, bool shift, bool ctrl)
        {
            return this.SetCurrentCell(dataGrid[this.GetBottomVisibleRow(dataGrid), this.GetRightMostVisibleColumn(dataGrid)], shift, ctrl);
        }

        public bool GoToLeftCell(DataGridCell currentCell)
        {
            return this.GoToLeftCell(currentCell, KeyboardUtil.Shift, KeyboardUtil.Ctrl);
        }

        public virtual bool GoToLeftCell(DataGridCell currentCell, bool shift, bool ctrl)
        {
            if (currentCell != null)
            {
                DataGrid dataGrid = currentCell.DataGrid;
                DataGridRow row = currentCell.Row;
                switch (row.Type)
                {
                    case DataGridRowType.Item:
                    case DataGridRowType.New:
                        {
                            DataGridColumn previousVisibleColumn = this.GetPreviousVisibleColumn(dataGrid.Viewport.Range(currentCell).TopLeftCell.Column);
                            if (previousVisibleColumn == null)
                            {
                                return false;
                            }
                            DataGridCell cell = dataGrid[row, previousVisibleColumn];
                            return this.SetCurrentCell(cell, shift, ctrl);
                        }
                    case DataGridRowType.Group:
                        ((DataGridGroupRow)row).GroupRowsVisibility = Visibility.Collapsed;
                        dataGrid.ScrollIntoView();
                        return true;
                }
            }
            return false;
        }

        public bool GoToLeftMostCell(DataGridCell currentCell)
        {
            return this.GoToLeftMostCell(currentCell, KeyboardUtil.Shift, KeyboardUtil.Ctrl);
        }

        public virtual bool GoToLeftMostCell(DataGridCell currentCell, bool shift, bool ctrl)
        {
            DataGridCell leftMostCell = this.GetLeftMostCell(currentCell);
            return this.SetCurrentCell(leftMostCell, shift, ctrl);
        }

        public bool GoToRightCell(DataGridCell currentCell)
        {
            return this.GoToRightCell(currentCell, KeyboardUtil.Shift, KeyboardUtil.Ctrl);
        }

        public virtual bool GoToRightCell(DataGridCell currentCell, bool shift, bool ctrl)
        {
            if (currentCell != null)
            {
                DataGrid dataGrid = currentCell.DataGrid;
                DataGridRow row = currentCell.Row;
                switch (row.Type)
                {
                    case DataGridRowType.Item:
                    case DataGridRowType.New:
                        {
                            DataGridColumn nextVisibleColumn = this.GetNextVisibleColumn(dataGrid.Viewport.Range(currentCell).BottomRightCell.Column);
                            if (nextVisibleColumn == null)
                            {
                                return false;
                            }
                            DataGridCell cell = dataGrid[row, nextVisibleColumn];
                            return this.SetCurrentCell(cell, shift, ctrl);
                        }
                    case DataGridRowType.Group:
                        ((DataGridGroupRow)row).GroupRowsVisibility = Visibility.Visible;
                        dataGrid.ScrollIntoView();
                        return true;
                }
            }
            return false;
        }

        public bool GoToRightMostCell(DataGridCell currentCell)
        {
            return this.GoToRightMostCell(currentCell, KeyboardUtil.Shift, KeyboardUtil.Ctrl);
        }

        public virtual bool GoToRightMostCell(DataGridCell currentCell, bool shift, bool ctrl)
        {
            DataGridCell rightMostCell = this.GetRightMostCell(currentCell);
            return this.SetCurrentCell(rightMostCell, shift, ctrl);
        }

        public virtual bool GoToTopCell(DataGridCell currentCell, bool shift, bool ctrl)
        {
            DataGridCell topCell = this.GetTopCell(currentCell);
            if (topCell != null)
            {
                this.SetCurrentCell(topCell, shift, ctrl);
                return true;
            }
            return false;
        }

        public bool GoToUpCell(DataGridCell currentCell)
        {
            return this.GoToUpCell(currentCell, KeyboardUtil.Shift, KeyboardUtil.Ctrl);
        }

        public virtual bool GoToUpCell(DataGridCell currentCell, bool shift, bool ctrl)
        {
            if (currentCell != null)
            {
                DataGrid dataGrid = currentCell.DataGrid;
                DataGridRow previousVisibleRow = this.GetPreviousVisibleRow(dataGrid.Viewport.Range(currentCell).TopLeftCell.Row);
                if (previousVisibleRow != null)
                {
                    return this.SetCurrentCell(currentCell.DataGrid[previousVisibleRow, currentCell.Column], shift, ctrl);
                }
            }
            return false;
        }

        public virtual void HandleKeyDown(System.Windows.Input.KeyEventArgs e)
        {
            bool handled = e.Handled;
            this.HandleKeyDown(e.Key, KeyboardUtil.Ctrl, KeyboardUtil.Shift, ref handled, e.OriginalSource, e);
            e.Handled = handled;
        }

        internal void HandleKeyDown(Key key, bool ctrl, bool shift, ref bool handled, object originalSource, System.Windows.Input.KeyEventArgs e)
        {
            try
            {
                bool flag;
                int index;
                DataGridCell previousEditableCell;
                if ((this.DataGrid.CurrentCell == null) || !this.DataGrid.CurrentCell.IsEditing)
                {
                    goto Label_01CF;
                }
                DataGridCell currentCell = this.DataGrid.CurrentCell;
                switch (key)
                {
                    case Key.Tab:
                        if (!shift)
                        {
                            goto Label_0134;
                        }
                        previousEditableCell = this.GetPreviousEditableCell(currentCell);
                        goto Label_013D;

                    case Key.Enter:
                        {
                            flag = false;
                            DataGridRow row = currentCell.Row;
                            index = row.Index;
                            if (!this.DataGrid.EndEditRow(true))
                            {
                                goto Label_00EF;
                            }
                            DataGridCell cell2 = this.DataGrid.CurrentCell;
                            if (cell2 == null)
                            {
                                break;
                            }
                            if ((row.Type == DataGridRowType.Item) && (index == cell2.Row.Index))
                            {
                                this.GoToDownCell(cell2);
                            }
                            goto Label_00EB;
                        }
                    case Key.Escape:
                        if (this.DataGrid.EndEdit(false, true))
                        {
                            handled = true;
                        }
                        return;

                    default:
                        return;
                }
                if ((index >= 0) && (index < this.DataGrid.Rows.Count))
                {
                    DataGridRow row2 = this.DataGrid.Rows[index];
                    if (row2 != null)
                    {
                        this.DataGrid.CurrentRow = row2;
                    }
                }
            Label_00EB:
                flag = true;
                goto Label_00F1;
            Label_00EF:
                flag = true;
            Label_00F1:
                if (flag)
                {
                    this.DataGrid.ScrollIntoView();
                    handled = true;
                }
                return;
            Label_0134:
                previousEditableCell = this.GetNextEditableCell(currentCell);
            Label_013D:
                if (((currentCell.Row is DataGridNewRow) && ((previousEditableCell == null) || (previousEditableCell.Row != currentCell.Row))) && this.DataGrid.EndNewRow(true))
                {
                    if (shift)
                    {
                        previousEditableCell = this.GetLastEditableCell(currentCell.Row);
                    }
                    else
                    {
                        previousEditableCell = this.GetFirstEditableCell(currentCell.Row);
                    }
                }
                if (previousEditableCell != null)
                {
                    this.SetCurrentCell(previousEditableCell, false, false);
                    this.DataGrid.BeginEdit();
                    handled = true;
                }
                else
                {
                    this.DataGrid.EndEdit(true, true);
                }
                return;
            Label_01CF:
                if (originalSource == this.DataGrid)
                {
                    if (((this.DataGrid.CurrentRow != null) && this.DataGrid.CurrentRow.IsEditing) && (key == Key.Escape))
                    {
                        if (this.DataGrid.EndEditRow(false))
                        {
                            handled = true;
                        }
                    }
                    else
                    {
                        switch (key)
                        {
                            case Key.Add:
                                if ((ctrl && (this.DataGrid.CurrentCell != null)) && this.DataGrid.CurrentCell.Row.ActualCanUserToggleDetails)
                                {
                                    this.DataGrid.CurrentCell.Row.DetailsVisibility = Visibility.Visible;
                                    handled = true;
                                }
                                break;

                            case Key.Subtract:
                                if (ctrl && (this.DataGrid.CurrentCell != null))
                                {
                                    this.DataGrid.CurrentCell.Row.DetailsVisibility = Visibility.Collapsed;
                                    handled = true;
                                }
                                break;

                            case Key.F2:
                                if (this.DataGrid.CanUserEditRows && this.DataGrid.BeginEdit())
                                {
                                    handled = true;
                                }
                                break;

                            case Key.V:
                                goto Label_05FD;

                            case Key.Enter:
                                if (this.DataGrid.EndNewRow(true))
                                {
                                    handled = true;
                                }
                                break;

                            case Key.PageUp:
                                if (this.DataGrid.PageUp(true))
                                {
                                    handled = true;
                                }
                                break;

                            case Key.PageDown:
                                if (this.DataGrid.PageDown(true))
                                {
                                    handled = true;
                                }
                                break;

                            case Key.End:
                                if (!ctrl)
                                {
                                    goto Label_041C;
                                }
                                if (this.GoToLastCell(this.DataGrid, shift, false))
                                {
                                    handled = true;
                                }
                                break;

                            case Key.Home:
                                if (!ctrl)
                                {
                                    goto Label_03DE;
                                }
                                if (this.GoToFirstCell(this.DataGrid, shift, false))
                                {
                                    handled = true;
                                }
                                break;

                            case Key.Left:
                                if (!ctrl)
                                {
                                    goto Label_02D7;
                                }
                                if (this.GoToLeftMostCell(this.DataGrid.CurrentCell, shift, false))
                                {
                                    handled = true;
                                }
                                break;

                            case Key.Up:
                                if (!ctrl)
                                {
                                    goto Label_035D;
                                }
                                if (this.GoToTopCell(this.DataGrid.CurrentCell, shift, false))
                                {
                                    handled = true;
                                }
                                break;

                            case Key.Right:
                                if (!ctrl)
                                {
                                    goto Label_031A;
                                }
                                if (this.GoToRightMostCell(this.DataGrid.CurrentCell, shift, false))
                                {
                                    handled = true;
                                }
                                break;

                            case Key.Down:
                                if (!ctrl)
                                {
                                    goto Label_03A0;
                                }
                                if (this.GoToBottomCell(this.DataGrid.CurrentCell, shift, false))
                                {
                                    handled = true;
                                }
                                break;

                            case Key.Insert:
                                if (this.DataGrid.BeginNewRow())
                                {
                                    handled = true;
                                }
                                break;

                            case Key.Delete:
                                goto Label_0455;

                            case Key.A:
                                goto Label_0642;

                            case Key.C:
                                goto Label_05CC;
                        }
                    }
                }
                return;
            Label_02D7:
                if (this.GoToLeftCell(this.DataGrid.CurrentCell))
                {
                    handled = true;
                }
                return;
            Label_031A:
                if (this.GoToRightCell(this.DataGrid.CurrentCell))
                {
                    handled = true;
                }
                return;
            Label_035D:
                if (this.GoToUpCell(this.DataGrid.CurrentCell))
                {
                    handled = true;
                }
                return;
            Label_03A0:
                if (this.GoToDownCell(this.DataGrid.CurrentCell))
                {
                    handled = true;
                }
                return;
            Label_03DE:
                if (this.GoToLeftMostCell(this.DataGrid.CurrentCell))
                {
                    handled = true;
                }
                return;
            Label_041C:
                if (this.GoToRightMostCell(this.DataGrid.CurrentCell))
                {
                    handled = true;
                }
                return;
            Label_0455:
                if (this.DataGrid.CanUserRemoveRows && this.DataGrid.IsRemovingRowsAllowed)
                {
                    try
                    {
                        this.DataGrid.RemoveRows((from row in this.DataGrid.Selection.SelectedRows
                                                  where row.Type == DataGridRowType.Item
                                                  select row).ToArray<DataGridRow>());
                    }
                    catch
                    {
                    }
                    handled = true;
                }
                return;
            Label_05CC:
                if (ctrl)
                {
                    try
                    {
                        if ((((byte)this.DataGrid.ClipboardCopyMode) & 1) == 1)
                        {
                            Clipboard.SetText(this.GetClipboardContent());
                            handled = true;
                        }
                    }
                    catch (SecurityException)
                    {
                    }
                }
                return;
            Label_05FD:
                if (ctrl && (this.DataGrid.CurrentCell != null))
                {
                    try
                    {
                        if ((((byte)this.DataGrid.ClipboardPasteMode) & 1) == 1)
                        {
                            string text = Clipboard.GetText();
                            this.SetClipboardContent(text);
                            handled = true;
                        }
                    }
                    catch
                    {
                    }
                }
                return;
            Label_0642:
                if (ctrl && this.DataGrid.Selection.SelectAll())
                {
                    handled = true;
                }
            }
            catch (DataGridFinalizeEditingException)
            {
                if (((this.DataGrid.CurrentCell != null) && this.DataGrid.CurrentCell.IsEditing) && ((this.DataGrid.CurrentCell.Presenter != null) && (this.DataGrid.CurrentCell.Presenter.Content is DependencyObject)))
                {
                    Control control = (this.DataGrid.CurrentCell.Presenter.Content as DependencyObject).GetFirstFocusableControl();
                    if (control != null)
                    {
                        this.DataGrid.Dispatcher.BeginInvoke(() => control.Focus());
                    }
                }
            }
            finally
            {
                if (((key == Key.Escape) || (key == Key.Enter)) || (key == Key.Back))
                {
                    handled = true;
                }
            }
        }

        public virtual void SetClipboardContent(string clipboardContent)
        {
            bool pasteRowHeaders = (((byte)this.DataGrid.ClipboardPasteMode) & 4) == 4;
            bool pasteColumnHeaders = (((byte)this.DataGrid.ClipboardPasteMode) & 2) == 2;
            this.SetFormattedText("\t", "\r\n", clipboardContent, pasteRowHeaders, pasteColumnHeaders, true);
        }

        public virtual bool SetCurrentCell(DataGridCell cell, bool shift, bool ctrl)
        {
            if (cell != null)
            {
                DataGrid dataGrid = cell.DataGrid;
                dataGrid.CurrentCell = cell;
                if (dataGrid.CurrentCell != null)
                {
                    cell = dataGrid.CurrentCell;
                    DataGridCellsRange range = dataGrid.Viewport.Range(cell);
                    this.SelectionManager.Select(range.TopLeftCell, range.BottomRightCell, shift, ctrl);
                    dataGrid.ScrollIntoView();
                    return true;
                }
            }
            return false;
        }

        protected void SetFormattedText(string separator, string lineSeparator, string clipboardContent, bool pasteRowHeaders, bool pasteColumnHeaders, bool updateSelection = true)
        {
            int index;
            DataGridRow row = this.DataGrid.Selection.SelectedRows.FirstOrDefault<DataGridRow>() ?? this.DataGrid.CurrentCell.Row;
            DataGridColumn column = this.DataGrid.Selection.SelectedColumns.FirstOrDefault<DataGridColumn>() ?? this.DataGrid.CurrentCell.Column;
            DataGridRow row2 = row;
            DataGridColumn column2 = column;
            DataGridRow row3 = row;
            DataGridColumn column3 = column;
            bool flag = true;
            bool flag2 = true;
            do
            {
                index = clipboardContent.IndexOf(lineSeparator);
                string str = (index >= 0) ? clipboardContent.Substring(0, index) : clipboardContent;
                clipboardContent = (index >= 0) ? clipboardContent.Substring(index + lineSeparator.Length) : "";
                if (!flag || !pasteColumnHeaders)
                {
                    int num2;
                    flag2 = true;
                    do
                    {
                        num2 = str.IndexOf(separator);
                        string str2 = (num2 >= 0) ? str.Substring(0, num2) : str;
                        str = (num2 >= 0) ? str.Substring(num2 + separator.Length) : "";
                        if (!flag2 || !pasteRowHeaders)
                        {
                            try
                            {
                                row3 = row2;
                                column3 = column2;
                                this.DataGrid[row2, column2].Value = str2;
                            }
                            catch
                            {
                            }
                            column2 = this.DataGrid.Columns.GetVisibleColumnsFromIndex(column2.DisplayIndex, GeneratorDirection.Forward).Skip<DataGridColumn>(1).FirstOrDefault<DataGridColumn>();
                        }
                        flag2 = false;
                    }
                    while ((num2 >= 0) && (column2 != null));
                    column2 = column;
                    row2 = this.DataGrid.Rows.GetVisibleRowsFromIndex(row2.Index, GeneratorDirection.Forward, false).Skip<DataGridRow>(1).FirstOrDefault<DataGridRow>();
                }
                flag = false;
            }
            while (((index >= 0) && (row2 != null)) && !string.IsNullOrEmpty(clipboardContent));
            if (updateSelection)
            {
                this.DataGrid.Selection.BeginUpdate();
                this.DataGrid.Selection.Clear();
                this.DataGrid.Selection.Add(this.DataGrid[row, column], this.DataGrid[row3, column3]);
                this.DataGrid.Selection.EndUpdate();
            }
        }

        // Properties
        public DataGrid DataGrid { get; private set; }

        public DataGridSelectionManager SelectionManager
        {
            get
            {
                return this.DataGrid.Model.SelectionManager;
            }
        }
    }
}
